package com.wdc.deliveryrobot.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.sirui.selfstudysdk.main.SelfChassisState;
import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.constant.Constants;
import com.wdc.deliveryrobot.dialog.TaskRunningDialog;
import com.wdc.deliveryrobot.event.Event;
import com.wdc.deliveryrobot.event.RobotSendEvent;
import com.wdc.deliveryrobot.utils.RunUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Objects;

public class TaskActivity extends BaseActivity{

    private ImageView imageView;

    private TextView textView;

    private JSONArray jsonArrayPoint;

    private final String TAG = "TaskActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_task);
        imageView = findViewById(R.id.loading_image);
        textView = findViewById(R.id.text_title);


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //屏幕被点击 停止导航
                SelfChassisState.getInstance().stopNavigation();
                TaskRunningDialog taskRunningDialog = new TaskRunningDialog(TaskActivity.this, new TaskRunningDialog.TaskRunningListener() {
                    @Override
                    public void cancelTask() {
                        Constants.TASK_NOW = null;
                        finish();
                    }

                    @Override
                    public void keepRunningTask() {
                        if (jsonArrayPoint != null){
                            for (int i = 0;i < jsonArrayPoint.size();i ++){
                                if (jsonArrayPoint.getJSONObject(i).get("name").equals(Constants.SEND_NOW_POINT)){
                                    RunUtils.Send(Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("x").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("y").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("yaw").toString()));
                                    break;
                                }
                            }
                        }
                    }
                });
                taskRunningDialog.sendStop();
                taskRunningDialog.show();
            }
        });
        // 使用 Glide 加载 GIF 动图
        Glide.with(this)
                .asGif()
                .load(R.drawable.loading) // 加载图片
                .into(imageView);


        initPoint();
        init();
    }
    private void initPoint(){
        JSONObject jsonObjectStations = selfChassisState.getStations();
        jsonArrayPoint = jsonObjectStations.getJSONArray("point");
    }
    private void init(){
        switch (Constants.TASK_NOW){
            case 1:
                //送物
                if (textView != null){
                    textView.setText(getString(R.string.send_now, Constants.SEND_NOW_POINT));
                    Log.d(TAG,"list:" + Constants.SEND_POINT + " now point : " + Constants.SEND_NOW_POINT );
                    if (jsonArrayPoint != null){
                        for (int i = 0;i < jsonArrayPoint.size();i ++){
                            if (jsonArrayPoint.getJSONObject(i).get("name").equals(Constants.SEND_NOW_POINT)){
                                RunUtils.Send(Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("x").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("y").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("yaw").toString()));
                                break;
                            }
                        }
                    }
                }
                break;
            case -1:
                Log.d(TAG,"错误");
                finish();
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSendTask(RobotSendEvent robotSendEvent){
        Log.d(TAG,"接受到eventBus");
        switch (robotSendEvent.getState()){
            case RobotSendEvent.PREPARE:
                Log.d(TAG,"出发");
                Toast.makeText(TaskActivity.this,"现在出发了",Toast.LENGTH_SHORT).show();
                break;
            case RobotSendEvent.ARRIVE:
                Log.d(TAG,"到达");
                TaskRunningDialog taskRunningDialog = new TaskRunningDialog(TaskActivity.this, new TaskRunningDialog.TaskRunningListener() {
                    @Override
                    public void cancelTask() {
                        Constants.TASK_NOW = null;
                        finish();
                    }

                    @Override
                    public void keepRunningTask() {
                        //继续任务
                        if (jsonArrayPoint != null){
                            if (textView != null){
                                textView.setText(getString(R.string.send_now, Constants.SEND_NOW_POINT));
                            }
                            for (int i = 0;i < jsonArrayPoint.size();i ++){
                                if (jsonArrayPoint.getJSONObject(i).get("name").equals(Constants.SEND_NOW_POINT)){
                                    RunUtils.Send(Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("x").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("y").toString()),Float.parseFloat(jsonArrayPoint.getJSONObject(i).get("yaw").toString()));
                                    break;
                                }
                            }
                        }
                    }
                });
                String point = null;
                if (Constants.SEND_POINT != null){
                    boolean isSet = false;
                    for (int i = 0 ;i < Constants.SEND_POINT.size(); i++){
                        if (isSet && Constants.SEND_POINT.get(i) != null){
                            Constants.SEND_NOW_POINT = Constants.SEND_POINT.get(i);
                            break;
                        }
                        if (Constants.SEND_POINT.get(i) != null && Constants.SEND_POINT.get(i).equals(Constants.SEND_NOW_POINT) && !isSet){
                            point = String.valueOf(i + 1);
                            Constants.SEND_NOW_POINT = null;
                            isSet = true;
                        }

                    }
                }
                if (Constants.SEND_NOW_POINT == null){
                    //最后一个点位
                    taskRunningDialog.sendArrived(point);
                    taskRunningDialog.show();
                }else {
                    taskRunningDialog.sendOneArrived(point);
                    taskRunningDialog.show();
                }
                break;
        }
    }
}
